shape <- readOGR('./TreesCityBoulder', layer='TreesCityBoulder')
## OGR data source with driver: ESRI Shapefile 
## Source: "./TreesCityBoulder", layer: "TreesCityBoulder"
## with 41839 features
## It has 38 fields
shape_latlon <- spTransform(shape, CRS("+proj=longlat +datum=WGS84"))
head(shape_latlon)
##      ID                    UNIQUEID ADDRESS SUFFIX  STREET   ONSTR
## 1 59084           JS 20150203131235     601   <NA> 10TH ST 10TH ST
## 2 59085           JS 20150203131431     601   <NA> 10TH ST 10TH ST
## 3 86480 DROID 201607141607903829845     601   <NA> 10TH ST 10TH ST
## 4 59087           JS 20150203131448     601   <NA> 10TH ST 10TH ST
## 5 59088           JS 20150203131959     690   <NA> 10TH ST 10TH ST
## 6 59089           JS 20150203132037     690   <NA> 10TH ST 10TH ST
##       FROMSTR       TOSTR  SIDE SITE              SPP DBH CULTIVAR
## 1 BASELINE RD  CUL-DE-SAC Front    1    Picea pungens   5     <NA>
## 2 BASELINE RD  CUL-DE-SAC Front    2    Picea pungens   2     <NA>
## 3 BASELINE RD  CUL-DE-SAC Front    3     Ulmus pumila   6     <NA>
## 4 BASELINE RD  CUL-DE-SAC Front    4 Acer platanoides   6     <NA>
## 5  CUL-DE-SAC BASELINE RD Front    1 Acer platanoides   7     <NA>
## 6  CUL-DE-SAC BASELINE RD Front    2 Acer platanoides   3     <NA>
##   CONDITION INSPECT                           MT     MT2     TTYPE MEMTREE
## 1 Excellent       N No Specific Maintenance Need Routine Evergreen      No
## 2      Good       N No Specific Maintenance Need Routine Evergreen      No
## 3      Dead       N No Specific Maintenance Need Routine Deciduous      No
## 4      Fair       N No Specific Maintenance Need Routine Deciduous      No
## 5      Fair       N No Specific Maintenance Need Routine Deciduous      No
## 6      Good       N No Specific Maintenance Need Routine Deciduous      No
##   DISEASEDEF   STRUTDEFEC LOCTYPE          SITECAT                  HOOD
## 1        N/A          N/A  Street Shrub /Mulch bed BSLO - Baseline South
## 2        N/A          N/A  Street Shrub /Mulch bed BSLO - Baseline South
## 3        N/A trunk wounds  Street Shrub /Mulch bed BSLO - Baseline South
## 4        N/A trunk wounds  Street Shrub /Mulch bed BSLO - Baseline South
## 5        N/A     deadwood  Street        Bare dirt BSLO - Baseline South
## 6        N/A          N/A  Street        Bare dirt BSLO - Baseline South
##   LOCATION JURISDIC TSIP    GRATESZ    GUARDSZ        EAB   INV_DATE
## 1   Street Forestry    N Unassigned Unassigned Unassigned 2015/02/03
## 2   Street Forestry    N Unassigned Unassigned Unassigned 2015/02/03
## 3   Street Forestry    N Unassigned Unassigned Unassigned 2016/07/14
## 4   Street Forestry    N Unassigned Unassigned Unassigned 2015/02/03
## 5   Street Forestry    N Unassigned Unassigned Unassigned 2015/02/03
## 6   Street Forestry    N Unassigned Unassigned Unassigned 2015/02/03
##   INV_TIME INSPECT_DT INSPECT_TM   NOTES ACTIVE   COMMONNAME GENUS
## 1     <NA> 2015/07/10   11:40:29    <NA>      1  Blue Spruce Picea
## 2     <NA> 2015/07/10   11:40:29    <NA>      1  Blue Spruce Picea
## 3     <NA> 2017/03/09   12:46:32  TC np.      1 Siberian Elm Ulmus
## 4     <NA> 2015/07/10   11:40:30    <NA>      1 Norway Maple  Acer
## 5     <NA> 2015/07/10   11:40:30    <NA>      1 Norway Maple  Acer
## 6     <NA> 2015/07/10   11:40:30 cluster      1 Norway Maple  Acer
csv <- read.csv('Trees_Public.csv')
head(csv)
##   FID    ID                    UNIQUEID ADDRESS SUFFIX  STREET   ONSTR
## 1   0 59084           JS 20150203131235     601        10TH ST 10TH ST
## 2   1 59085           JS 20150203131431     601        10TH ST 10TH ST
## 3   2 86480 DROID 201607141607903829845     601        10TH ST 10TH ST
## 4   3 59087           JS 20150203131448     601        10TH ST 10TH ST
## 5   4 59088           JS 20150203131959     690        10TH ST 10TH ST
## 6   5 59089           JS 20150203132037     690        10TH ST 10TH ST
##       FROMSTR       TOSTR  SIDE SITE              SPP DBH CULTIVAR
## 1 BASELINE RD  CUL-DE-SAC Front    1    Picea pungens   5         
## 2 BASELINE RD  CUL-DE-SAC Front    2    Picea pungens   2         
## 3 BASELINE RD  CUL-DE-SAC Front    3     Ulmus pumila   6         
## 4 BASELINE RD  CUL-DE-SAC Front    4 Acer platanoides   6         
## 5  CUL-DE-SAC BASELINE RD Front    1 Acer platanoides   7         
## 6  CUL-DE-SAC BASELINE RD Front    2 Acer platanoides   3         
##   CONDITION INSPECT                           MT     MT2     TTYPE MEMTREE
## 1 Excellent       N No Specific Maintenance Need Routine Evergreen      No
## 2      Good       N No Specific Maintenance Need Routine Evergreen      No
## 3      Dead       N No Specific Maintenance Need Routine Deciduous      No
## 4      Fair       N No Specific Maintenance Need Routine Deciduous      No
## 5      Fair       N No Specific Maintenance Need Routine Deciduous      No
## 6      Good       N No Specific Maintenance Need Routine Deciduous      No
##   DISEASEDEF   STRUTDEFEC LOCTYPE          SITECAT                  HOOD
## 1        N/A          N/A  Street Shrub /Mulch bed BSLO - Baseline South
## 2        N/A          N/A  Street Shrub /Mulch bed BSLO - Baseline South
## 3        N/A trunk wounds  Street Shrub /Mulch bed BSLO - Baseline South
## 4        N/A trunk wounds  Street Shrub /Mulch bed BSLO - Baseline South
## 5        N/A     deadwood  Street        Bare dirt BSLO - Baseline South
## 6        N/A          N/A  Street        Bare dirt BSLO - Baseline South
##   LOCATION JURISDIC TSIP    GRATESZ    GUARDSZ        EAB
## 1   Street Forestry    N Unassigned Unassigned Unassigned
## 2   Street Forestry    N Unassigned Unassigned Unassigned
## 3   Street Forestry    N Unassigned Unassigned Unassigned
## 4   Street Forestry    N Unassigned Unassigned Unassigned
## 5   Street Forestry    N Unassigned Unassigned Unassigned
## 6   Street Forestry    N Unassigned Unassigned Unassigned
##              INV_DATE INV_TIME          INSPECT_DT INSPECT_TM   NOTES
## 1 2015-02-03 00:00:00       NA 2015-07-10 00:00:00   11:40:29        
## 2 2015-02-03 00:00:00       NA 2015-07-10 00:00:00   11:40:29        
## 3 2016-07-14 00:00:00       NA 2017-03-09 00:00:00   12:46:32  TC np.
## 4 2015-02-03 00:00:00       NA 2015-07-10 00:00:00   11:40:30        
## 5 2015-02-03 00:00:00       NA 2015-07-10 00:00:00   11:40:30        
## 6 2015-02-03 00:00:00       NA 2015-07-10 00:00:00   11:40:30 cluster
##   ACTIVE   COMMONNAME GENUS
## 1      1  Blue Spruce Picea
## 2      1  Blue Spruce Picea
## 3      1 Siberian Elm Ulmus
## 4      1 Norway Maple  Acer
## 5      1 Norway Maple  Acer
## 6      1 Norway Maple  Acer
summary(csv$EAB)
##           Group A           Group B           Group C Private Treatment 
##               226               565               534               127 
##           Removal        Unassigned 
##                41             49176
#ggplot(data=csv) + geom_bar(mapping = aes(x = GENUS))
filtered <- shape_latlon[shape_latlon$EAB != 'Unassigned',]
pal = colorFactor(palette = "RdYlBu", domain=filtered$EAB)
leaflet(data=filtered) %>% addProviderTiles(providers$CartoDB.Positron) %>% 
  addCircles(color=~pal(filtered$EAB)) %>% addLegend("bottomright", pal=pal, filtered$EAB)
filtered <- shape_latlon[shape_latlon$COMMONNAME == "Green Ash" | shape_latlon$COMMONNAME == "White Ash",]
pal = colorFactor(palette = "RdYlBu", domain=filtered$COMMONNAME)
leaflet(data=filtered) %>% addProviderTiles(providers$CartoDB.Positron) %>% 
  addCircles(color=~pal(filtered$COMMONNAME)) %>% addLegend("bottomright", pal=pal, filtered$COMMONNAME)
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette RdYlBu is 11
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette RdYlBu is 11
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette RdYlBu is 11
## Returning the palette you asked for with that many colors
filtered <- shape_latlon[shape_latlon$COMMONNAME == "Green Ash" | shape_latlon$COMMONNAME == "White Ash",]
pal = colorFactor(palette = "RdYlBu", domain=c("Dead","Very Poor", "Poor", "Fair", "Good", "Excellent", "N/A"))
leaflet(data=filtered) %>% addProviderTiles(providers$CartoDB.Positron) %>% 
  addCircles(color=~pal(filtered$CONDITION)) %>% addLegend("bottomright", pal=pal, filtered$CONDITION)
library(leaflet.extras)
## Warning: package 'leaflet.extras' was built under R version 3.4.4
shape_latlon$is_ash <- shape_latlon$COMMONNAME == "Green Ash" | shape_latlon$COMMONNAME == "White Ash"
pal = colorFactor(palette = topo.colors(2), domain=c("TRUE", "FALSE"))
leaflet(data=filtered) %>% addProviderTiles(providers$CartoDB.Positron) %>% addHeatmap(radius=6)
  #addCircles(color=~pal(shape_latlon$is_ash)) %>% 
  #addLegend("bottomright", pal=pal, shape_latlon$is_ash) 
subdivisions <- readOGR('./ACS1216_bg', layer='ACS1216_bg')
## OGR data source with driver: ESRI Shapefile 
## Source: "./ACS1216_bg", layer: "ACS1216_bg"
## with 3532 features
## It has 257 fields
subdivisions_latlong <- spTransform(subdivisions, CRS("+proj=longlat +datum=WGS84"))
citylims <- readOGR('./Boulder_City_Limits', layer='Boulder_City_Limits')
## OGR data source with driver: ESRI Shapefile 
## Source: "./Boulder_City_Limits", layer: "Boulder_City_Limits"
## with 5 features
## It has 3 fields
## Integer64 fields read as strings:  OBJECTID
citylims_latlong <- spTransform(citylims, CRS("+proj=longlat +datum=WGS84"))
#leaflet(data=citylims_latlong) %>% addProviderTiles(providers$CartoDB.Positron) %>% addPolygons()
filtered_blocks <- crop(subdivisions_latlong, citylims_latlong)
blocks_objectid <- filtered_blocks[c("OBJECTID")]
block_ref <- intersect(shape_latlon, blocks_objectid)
summary(block_ref$d)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     105    1363    2695    2094    2897    3491
pal = colorFactor(palette = "RdYlBu", domain=block_ref$d)
leaflet(data=block_ref[block_ref$COMMONNAME == "White Ash" | block_ref$COMMONNAME == "Green Ash",]) %>% addProviderTiles(providers$CartoDB.Positron) %>% addPolygons(data=filtered_blocks) %>% addCircles(data = block_ref, color = ~pal(block_ref$d))
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette RdYlBu is 11
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette RdYlBu is 11
## Returning the palette you asked for with that many colors
#data.frame(block_ref)
block_gb <- data.frame(block_ref) %>% group_by(d) %>% tally()
block_gb_ash <- data.frame(block_ref[block_ref$COMMONNAME == "White Ash" | block_ref$COMMONNAME == "Green Ash",]) %>% group_by(d) %>% tally()
block_gb_merge <- merge(block_gb, block_gb_ash, by="d")
block_gb_merge$ratio <- block_gb_merge$n.y/block_gb_merge$n.x
qplot(block_gb_merge$ratio, geom="histogram", bins=12)

block_gb_merge$zscore <- (block_gb_merge$ratio-mean(block_gb_merge$ratio))/sd(block_gb_merge$ratio)
sp_blocks_zscore <- merge(blocks_objectid, block_gb_merge, by.x="OBJECTID", by.y="d")
colors <- colorBin("PRGn", domain = sp_blocks_zscore$zscore, bins = c(-3,-2,-1,0,1,2,3))
leaflet(sp_blocks_zscore) %>% addProviderTiles(providers$CartoDB.Positron) %>% addPolygons(color=colors(sp_blocks_zscore$zscore), weight=3, fillOpacity = .7) %>% addLegend("bottomright", 
            pal = colors, 
            values = sp_blocks_zscore$zscore, 
            title = "Z-score of % of ash trees")# %>% addHeatmap(data=filtered, radius=6)
## Warning in colors(sp_blocks_zscore$zscore): Some values were outside the
## color scale and will be treated as NA